// 实现为单例模式
// 无环回，无FIFO，因此依赖SBUS帧间隔作为中断依据
// 仅实现了RX功能 20230126

#ifndef __IO_SBUS_H
#define __IO_SBUS_H

#include <string.h>
#include <stdint.h>
#include "../common_typedef.h"

#define SBUS_Freq 50
#define SBUS_RAW_LEN 25
#define SBUS_CH_MAX 18
#define SBUS_CH_VAL_MAX 2000
#define SBUS_CH_VAL_MIN 0
#define SBUS_CH_VAL_DEFAULT ((SBUS_CH_VAL_MAX + SBUS_CH_VAL_MIN) / 2)

typedef enum
{
	NO_ERR,
	RX_LOST,
	TX_ADC_ERROR,
	MST_CFG_ERR,
	CFG_FILE_LOST,
	FRM_ERROR
} sbusError;

typedef enum
{
	SBUS_COM_FAIL_START,
	SBUS_COM_FAIL_END
} sbusComFailSta;

typedef struct
{
	int16_t bias;
	uint16_t failSafeVal;
	uint8_t ifInvert;
	uint8_t chMatch;
} SBusCfg;

typedef struct
{
	const uint8_t *raw;
	uint16_t ch[SBUS_CH_MAX];
	SBusCfg cfg[SBUS_CH_MAX];
	sbusComFailSta comFailSta;
} SBusObj;

sbusError SBusInit(/*const uint8_t *rom, int romLen,*/ const uint8_t *rxBuffer); // 初始化sbus单例
sbusError SBusEncode(uint16_t *raw);											 // 发射机部分代码仅保留接口，尚未实现
sbusError SBusDecode(int rxLen);
uint16_t GetSbus(uint8_t ch);
sbusComFailSta SBusIsComFail(void);

#endif
